package com.youku.plugins.endcardV4New.view
{
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.PluginEventDispather;
   import com.youku.plugins.endcardV4New.model.data.PlayerSizeData;
   import com.youku.ExternalProxy;
   import flash.utils.Timer;
   import flash.display.MovieClip;
   import com.youku.plugins.endcardV4New.model.RelativeVideoListProxy;
   import com.youku.events.PlayerControlEvent;
   import com.youku.plugins.endcardV4New.EndCardEvent;
   import flash.events.TimerEvent;
   import com.youku.plugins.endcardV4New.EndCardConstant;
   import com.youku.utils.ReportUtil;
   import com.youku.events.ReportEvent;
   import flash.utils.setTimeout;
   import com.youku.events.PluginEvent;
   import com.youku.plugins.endcardV4New.model.data.UpDownData;
   import com.youku.utils.OpenWindow;
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   import flash.net.URLVariables;
   import com.adobe.json.JSONEncoder;
   import flash.net.URLRequestMethod;
   import com.youku.data.PlayerConstant;
   import com.youku.plugins.endcardV4New.model.data.FavoriteData;
   import com.youku.plugins.endcardV4New.model.data.RelativeVideoData;
   
   public class EndCardMediator extends Object
   {
      
      public function EndCardMediator(param1:MovieClip, param2:IPlayerProxy, param3:PluginEventDispather, param4:RelativeVideoListProxy) {
         super();
         this._mainMc = param1;
         this._endCard = new EndCard();
         this._playerProxy = param2;
         this._endCardEventManager = param3;
         this._dataProxy = param4;
         this._externalProxy = new ExternalProxy();
         this._timer = new Timer(2 * 1000,1);
         this.init();
      }
      
      private var _playerProxy:IPlayerProxy;
      
      private var _endCardEventManager:PluginEventDispather;
      
      private var _sizeData:PlayerSizeData;
      
      private var _endCard:EndCard;
      
      private var _externalProxy:ExternalProxy;
      
      private var _timer:Timer;
      
      private var _mainMc:MovieClip;
      
      private var _dataProxy:RelativeVideoListProxy;
      
      private function init() : void {
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_END_CARD,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.HIDE_END_CARD,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.END_CARD_SET_FAVORITE,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.AFTER_CLOSE_PANELS,this.onPlayerEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_RELATIVE_VIDEO_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_REPLAY_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_SHARE_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_FAVORITE_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_NAVIGATE_LEFT_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_NAVIGATE_RIGHT_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_UP_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_DOWN_CLICK,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_FULLSCREEN_TAP,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.RELATIVE_DATA_COMPLETE,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.RELATIVE_DATA_ERROR,this.onEndCardEvent);
         this._endCardEventManager.addEventListener(EndCardEvent.ENDCARD_RESET,this.onEndCardEvent);
         this._timer.addEventListener(TimerEvent.TIMER_COMPLETE,this.onTimeComplete);
         this._sizeData = PlayerSizeData.getInstance();
      }
      
      private function onTimeComplete(param1:TimerEvent) : void {
         if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
         {
            ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_END_SHOW);
         }
         else
         {
            ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_END_SHOW);
         }
         this._timer.stop();
      }
      
      private function onEndCardEvent(param1:EndCardEvent) : void {
         var _loc3_:String = null;
         var _loc2_:* = false;
         switch(param1.type)
         {
            case EndCardEvent.RELATIVE_DATA_COMPLETE:
            case EndCardEvent.RELATIVE_DATA_ERROR:
               this._sizeData.winType = this._playerProxy.rootGetData.rootWinType;
               this._sizeData.playerType = EndCardConstant.PLAYER_TYPE_SITE;
               if(this._playerProxy.playerData.isTouchPlayer)
               {
                  this._sizeData.playerType = EndCardConstant.PLAYER_TYPE_TOUCH;
               }
               this._timer.reset();
               this._timer.start();
               this._endCard.show(param1.data,this._endCardEventManager,this._sizeData);
               break;
            case EndCardEvent.ENDCARD_RELATIVE_VIDEO_CLICK:
               _loc2_ = true;
               this._playerProxy.dispatchEvent(new ReportEvent(ReportEvent.ENDCARD_RELATED_REPORT,this.getReportString(param1.data)));
               if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_RELATED_COMMEND);
                  this.changeFullScreen();
                  setTimeout(this.gotoURL,600,param1.data.link);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_RELATED_COMMEND);
                  this.changeFullScreen();
                  this.gotoURL(param1.data.link);
               }
               break;
            case EndCardEvent.ENDCARD_REPLAY_CLICK:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ENDCARD_REPLAY));
               if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_REPLAY);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_REPLAY);
               }
               break;
            case EndCardEvent.ENDCARD_SHARE_CLICK:
               this._endCardEventManager.dispatchEvent(new EndCardEvent(EndCardEvent.ENDCARD_SHARE_BACKGROUND_SHOW));
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ENDCARD_SHARE));
               if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_SHARE);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_SHARE);
               }
               break;
            case EndCardEvent.ENDCARD_NAVIGATE_LEFT_CLICK:
               if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_TURN_UP);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_TURN_UP);
               }
               break;
            case EndCardEvent.ENDCARD_NAVIGATE_RIGHT_CLICK:
               if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_TURN_DOWN);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_TURN_DOWN);
               }
               break;
            case EndCardEvent.ENDCARD_UP_CLICK:
               this._playerProxy.fsoProxy.saveUpDownInfo(this._playerProxy.playerData.videoIdEncode,new Date().getTime(),"up");
               UpDownData.upDown = "up";
               this.postUpDown("up");
               if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_UP);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_UP);
               }
               break;
            case EndCardEvent.ENDCARD_DOWN_CLICK:
               this._playerProxy.fsoProxy.saveUpDownInfo(this._playerProxy.playerData.videoIdEncode,new Date().getTime(),"down");
               UpDownData.upDown = "down";
               this.postUpDown("down");
               ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_DOWN);
               break;
            case EndCardEvent.ENDCARD_FULLSCREEN_TAP:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ENDCARD_CHANGE_FULLSCREEN));
               break;
            case EndCardEvent.ENDCARD_FAVORITE_CLICK:
               if(this._playerProxy.rootGetData.rootWinType == EndCardConstant.WINTYPE_INTERIOR)
               {
                  this.changeFullScreen();
                  this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ENDCARD_FAVORITE_CLICK));
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_INSITE_FAVORITE);
               }
               else
               {
                  this.changeFullScreen();
                  _loc2_ = true;
                  this.gotoURL(this._playerProxy.playerData.getJumpURL());
                  ReportUtil.sendUserActionLog(ReportUtil.END_INTERFACE_OUTSITE_FAVORITE);
               }
               break;
            case EndCardEvent.ENDCARD_RESET:
               if(this._mainMc.contains(this._endCard))
               {
                  this._endCard.hide();
                  this._mainMc.removeChild(this._endCard);
               }
               setTimeout(this.resetEndCard,1000);
               break;
         }
         if(_loc2_ = true)
         {
            if(this._playerProxy.rootGetData.rootWinType == "exterior" || this._playerProxy.rootGetData.rootWinType == "BDskin")
            {
               if(OpenWindow.errorInfo != null)
               {
                  _loc3_ = OpenWindow.errorInfo;
                  this._playerProxy.dispatchEvent(new ReportEvent(ReportEvent.JUMP_URL_ERROR,_loc3_));
               }
            }
         }
      }
      
      private function resetEndCard() : void {
         this._mainMc.addChild(this._endCard);
         this._dataProxy.loadData(this._playerProxy);
      }
      
      private function postUpDown(param1:String) : void {
         var _loc2_:URLLoader = new URLLoader();
         var _loc3_:* = "http://v.youku.com/QVideo/~ajax/updown?__rt=1&__ro=";
         var _loc4_:URLRequest = new URLRequest(_loc3_);
         var _loc5_:URLVariables = new URLVariables();
         var _loc6_:String = this._playerProxy.playerData.tsup;
         var _loc7_:String = this._playerProxy.playerData.hashcashup;
         var _loc8_:JSONEncoder = new JSONEncoder(
            {
               "videoId":this._playerProxy.playerData.videoIdEncode,
               "type":param1,
               "t":_loc6_,
               "s":_loc7_
            });
         _loc5_.__ap = _loc8_.getString();
         _loc4_.data = _loc5_;
         _loc4_.method = URLRequestMethod.POST;
         _loc2_.load(_loc4_);
      }
      
      private function changeFullScreen() : void {
         if(this._sizeData.isFullScreen)
         {
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ENDCARD_CHANGE_FULLSCREEN));
         }
      }
      
      private function gotoURL(param1:String) : void {
         var _loc2_:String = param1;
         if(this._playerProxy.rootGetData.rootLight == "off")
         {
            _loc2_ = _loc2_ + "?l=off";
         }
         if(this._playerProxy.rootGetData.rootWinType != "interior")
         {
            OpenWindow.open(new URLRequest(_loc2_),"_blank");
         }
         else
         {
            OpenWindow.open(new URLRequest(_loc2_),"_self");
         }
      }
      
      private function onPlayerEvent(param1:PlayerControlEvent) : void {
         var panel:String = null;
         var state:Boolean = false;
         var e:PlayerControlEvent = param1;
         switch(e.type)
         {
            case PlayerControlEvent.SHOW_END_CARD:
               this._mainMc.addChild(this._endCard);
               this._dataProxy.loadData(this._playerProxy);
               break;
            case PlayerControlEvent.HIDE_END_CARD:
            case PlayerControlEvent.COMMON_RESET:
               this._endCard.hide();
               if(this._mainMc.contains(this._endCard))
               {
                  this._mainMc.removeChild(this._endCard);
               }
               break;
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
            case PlayerControlEvent.FULLSCREEN_CHANGED:
               this._sizeData.data = e.data;
               try
               {
                  this._endCard.changeSize(this._sizeData);
               }
               catch(error:Error)
               {
               }
               break;
            case PlayerControlEvent.AFTER_CLOSE_PANELS:
               panel = e.data.panel;
               if(panel == PlayerConstant.PANEL_SHARE)
               {
                  this._endCardEventManager.dispatchEvent(new EndCardEvent(EndCardEvent.ENDCARD_SHARE_BACKGROUND_HIDE));
               }
               break;
            case PlayerControlEvent.END_CARD_SET_FAVORITE:
               state = e.data.state;
               FavoriteData.isFavorite = state;
               this._endCardEventManager.dispatchEvent(new EndCardEvent(EndCardEvent.ENDCARD_FAVORITE_STATE_CHANGE));
               break;
         }
      }
      
      private function getReportString(param1:Object) : String {
         var _loc2_:RelativeVideoData = param1.rData;
         var _loc3_:String = String(param1.ID);
         var _loc4_:String = _loc2_.ct;
         var _loc5_:String = _loc2_.dct;
         var _loc6_:* = "1";
         var _loc7_:* = "1";
         var _loc8_:String = _loc2_.md;
         var _loc9_:String = _loc2_.ord == null?"":_loc2_.ord;
         var _loc10_:String = _loc2_.rtlid;
         var _loc11_:String = _loc2_.dvid;
         var _loc12_:String = _loc2_.dsid;
         var _loc13_:String = _loc2_.abver;
         var _loc14_:String = _loc2_.dma;
         var _loc15_:String = _loc2_.algInfo;
         if(this._playerProxy.playerData.playmode == "normal")
         {
            _loc7_ = "1";
         }
         else if(this._playerProxy.playerData.playmode == "show")
         {
            _loc7_ = "3";
         }
         else if(this._playerProxy.playerData.playmode == "folder")
         {
            _loc7_ = "4";
         }
         
         
         if(this._playerProxy.rootGetData.rootWinType == "interior")
         {
            _loc6_ = "1";
         }
         else
         {
            _loc6_ = "8";
            _loc7_ = "1";
         }
         var _loc16_:String = "pos=" + _loc3_ + "&" + "sct=" + _loc4_ + "&" + "dct=" + _loc5_ + "&" + "apt=" + _loc6_ + "&" + "pg=" + _loc7_ + "&" + "md=" + _loc8_ + "&" + "ord=" + _loc9_ + "&" + "rtlid=" + _loc10_ + "&" + "dvid=" + _loc11_ + "&" + "dsid=" + _loc12_ + "&" + "abver=" + _loc13_ + "&" + "dma=" + _loc14_ + "&" + "algInfo=" + _loc15_ + "&" + "r=" + String(new Date().getTime());
         return _loc16_;
      }
   }
}
